package com.yumeng.framework.persistence.mybatis.base.page;


import com.yumeng.common.api.PageData;
import com.yumeng.common.exception.YumengException;
import com.yumeng.framework.persistence.mybatis.base.page.param.PageParam;

import java.util.List;
import java.util.function.Function;

/**
 * 暂未找到使用场景
 *
 * @author wxd
 * @date 2023/5/22 15:45
 */
@Deprecated
public class PageHelper {

    private static final Integer MAX_PAGE_NUM = 10000;

    /**
     * 获取下一页数据
     *
     * @param param
     * @param func
     * @return
     * @param <T>
     */
    public static <T> List<T> getNextList(PageParam param, int index, Function<PageParam, PageData<T>> func){
        return getNextList(param, true, index, MAX_PAGE_NUM, func);
    }

    /**
     * 获取下一页数据
     * @param param
     * @param moveNext
     * @param index
     * @param func
     * @return
     * @param <T>
     */
    public static <T> List<T> getNextList(PageParam param, boolean moveNext, int index, Function<PageParam, PageData<T>> func){
        return getNextList(param, moveNext, index, MAX_PAGE_NUM, func);
    }

    /**
     * 获取下一页数据
     *
     * @param param
     * @param func
     * @return
     * @param <T>
     */
    public static <T> List<T> getNextList(PageParam param, boolean moveNext, int index, Integer maxPage, Function<PageParam, PageData<T>> func){
        if (maxPage == null){
            maxPage = MAX_PAGE_NUM;
        }
        if (index > maxPage){
            //正常分页循环获取列表不会有这么多页
            throw new YumengException("当index>%d时抛出，防止死循环", maxPage);
        }
        PageData<T> page = func.apply(param);
        if (moveNext){
            param.moveNextPage();
        }
        return page.getRecords();
    }

}
